import {observable, action} from 'mobx';
import {WeaLocaleProvider} from 'ecCom';
import * as API_WFREPORT from '../../apis/efficiencyReport';
import * as ReportUtil from '../../util/reportUtil';

const getLabel = WeaLocaleProvider.getLabel;

export class WfDoingAnalyseStore {
    @observable loading = false;

    @observable advanceSearchData = {};
    @observable advanceSearchPara = {
        orgtype: 3,
        orgid: '',
        datarange: 1,
        dataid: '',
        createdatetype: 1,
        createstartdate: '',
        createenddate: '',
        receivedatetype: 1,
        receivestartdate: '',
        receiveenddate: '',
        tabKey: 1,
    };
    @observable analyseData = {};
    @observable modalParams = {};

    constructor() {
        this.initDatas = this.initDatas.bind(this);
        this.updateAdvanceSearchPara = this.updateAdvanceSearchPara.bind(this);
        this.loadAnalyseData = this.loadAnalyseData.bind(this);
        this.reSet = this.reSet.bind(this);
        this.showModal = this.showModal.bind(this);
        this.closeModal = this.closeModal.bind(this);
        this.updateModalPara = this.updateModalPara.bind(this);
        this.clearStatus = this.clearStatus.bind(this);
    }

    @action
    initDatas() {
        API_WFREPORT.getWfDoingAnalyseSearchCondition({}).then((data) => {
            this.advanceSearchData = data;
            const {noRight = false, rightKey = '-100'} = data;
            if (!noRight) {
                this.updateAdvanceSearchPara({rightKey});
            }
        });

    }

    loadAnalyseData() {
        this.loading = true;
        Promise.all([
            API_WFREPORT.getWfDoingAnalyseCountData({...this.advanceSearchPara}).then(data => {
                this.analyseData = {...this.analyseData, ...data};
            }),
            API_WFREPORT.getWfDoingAnalyseEchartData({...this.advanceSearchPara}).then(data => {
                this.analyseData = {...this.analyseData, ...data};
            })
        ]).then(result => {
            this.loading = false;
        });
    }

    updateAdvanceSearchPara(para) {
        let isload = true;
        let defaultInfo = {};
        let iddoupdate = false;
        for (let o in para) {
            if (this.advanceSearchPara[o] != para[o]) {
                iddoupdate = true;
                if (o === 'orgtype') {
                    const orgtype = para[o];
                    if (orgtype > 1) {
                        const {config = []} = this.advanceSearchData;
                        if (config.length > 0) {
                            const {selectLinkageDatas = {}} = config[0];
                            defaultInfo.orgid = ReportUtil.getDefaultId(orgtype, selectLinkageDatas);
                        }
                    }
                }
                if (o === 'datarange' && para[o] > 1) isload = false;
                if (o === 'datarange' && para[o] == 1) {
                    defaultInfo.dataid = '';
                }
                if (o === 'createdatetype') {
                    if (para.createdatetype == '2') {
                        isload = false;
                    } else {
                        defaultInfo.createstartdate = '';
                        defaultInfo.createenddate = '';
                    }
                }
                if (o === 'receivedatetype') {
                    if (para.receivedatetype == '2') {
                        isload = false;
                    } else {
                        defaultInfo.receivestartdate = '';
                        defaultInfo.receiveenddate = '';
                    }
                }
                if (o === 'orgid' && para[o] == '') isload = false;
                if (o === 'dataid' && para[o] == '' && (para.datarange > 1 || (para.datarange > 1 && this.advanceSearchPara.datarange > 1))) {
                    isload = false;
                }
                if (o === 'createstartdate' && para[o] == '' && (para.createdatetype == '2' || (para.createdatetype == '2' && this.advanceSearchPara.createdatetype == '2'))) {
                    isload = false;
                }
                if (o === 'receivestartdate' && para[o] == '' && (para.receivedatetype == '2' || (para.receivedatetype == '2' && this.advanceSearchPara.receivedatetype == '2'))) {
                    isload = false;
                }
            }
        }
        if (iddoupdate) {
            this.advanceSearchPara = {...this.advanceSearchPara, ...para, ...defaultInfo};
            if (isload) {
                this.loadAnalyseData();
            }
        }
    }

    //type 1:更多 2:reqlist 3:count
    showModal(type, para = {}) {
        if (type == 1) {
            const {module = 1} = para;
            const dataParams = {
                'api': 'getWfDoingAnalyseEchartMore',
                ...this.advanceSearchPara,
                ...para
            }
            const modalTitle = module == 1 ? getLabel(131930, '待办数量排名') : getLabel(131999, '最多待办流程路径');
            const clickParams = {module};
            this.modalParams = {
                modalVisible: true,
                modaltitle: modalTitle,
                isRowClick: true,
                dataParams,
                clickParams
            }
        } else if (type == 2 || type == 3) {
            const {module, key = ''} = para;
            const dataParams = {
                'api': 'getWfDoingAnalyseRequestList',
                ...this.advanceSearchPara,
                module,
                keyid: key
            }
            this.modalParams = {
                modalVisible: true,
                modaltitle: getLabel(383302, '具体流程清单'),
                dataParams,
                needAdvacnceSearch: true
            }
        }
    }

    updateModalPara(para = {}) {
        const {module, keyid = ''} = para;
        const dataParams = {
            'api': 'getWfDoingAnalyseRequestList',
            ...this.advanceSearchPara,
            module,
            keyid
        }
        this.modalParams = {
            modalVisible: true,
            modaltitle: getLabel(383302, '具体流程清单'),
            dataParams,
            needAdvacnceSearch: true
        }
    }

    reSet() {
        this.advanceSearchPara = {
            orgtype: 3,
            orgid: '',
            datarange: 1,
            dataid: '',
            createdatetype: 1,
            createstartdate: '',
            createenddate: '',
            receivedatetype: 1,
            receivestartdate: '',
            receiveenddate: '',
            tabKey: 1,
        };
    }

    closeModal() {
        this.modalParams = {modalVisible: false};
    }

    clearStatus() {
        this.loading = false;
        this.advanceSearchData = {};
        this.advanceSearchPara = {
            orgtype: 3,
            orgid: '',
            datarange: 1,
            dataid: '',
            createdatetype: 1,
            createstartdate: '',
            createenddate: '',
            receivedatetype: 1,
            receivestartdate: '',
            receiveenddate: '',
            tabKey: 1,
        };
        this.analyseData = {};
        this.modalParams = {};
    }
}
